<?
	/* Vimeo - stream reader plugin */	

	// return type of service
	function vimeo_getType() { return "media"; }


	// extra the service- action  line ("ike: "I tweeted on twitter.com")
	function vimeo_getAction($line) {
		$entry=explode("|",$line);
		
		$entry[7]=trim($entry[7]);
		if ($entry[7]=="uploads") return "<a href='?service=vimeo'><img class='icon' src='".serviceIcon("vimeo")."'/></a> I uploaded a movie to <a href='http://vimeo.com'>vimeo.com</a>";
		if ($entry[7]=="likes") return "<a href='?service=vimeo'><img class='icon' src='".serviceIcon("vimeo")."'/></a> I liked a movie on <a href='http://vimeo.com'>vimeo.com</a>";
	}

	// extract and return the correct caption
	function vimeo_getCaption($line) {
		$entry=explode("|",$line);
		return null; //$entry[6];	// caption specified in vimeo movie itself
	}

	// extract and return the correct post
	function vimeo_getPost($line) {
		$entry=explode("|",$line);
		$itemPost='<object width="400" height="300"><param name="allowfullscreen" value="true" /><param name="allowscriptaccess" value="always" /><param name="movie" value="http://vimeo.com/moogaloop.swf?clip_id='.$entry[3].'&amp;server=vimeo.com&amp;show_title=1&amp;show_byline=1&amp;show_portrait=0&amp;color=&amp;fullscreen=1" /><embed src="http://vimeo.com/moogaloop.swf?clip_id='.$entry[3].'&amp;server=vimeo.com&amp;show_title=1&amp;show_byline=1&amp;show_portrait=0&amp;color=&amp;fullscreen=1" type="application/x-shockwave-flash" allowfullscreen="true" allowscriptaccess="always" width="400" height="300"></embed></object>';

		return $itemPost;
	}

	// extract and return the correct link to the item
	function vimeo_getLink($line) {
		$entry=explode("|",$line);
		return 'http://www.vimeo.com/'.$entry[3];
	}


	// display information about us with a link to the profile
	//  -- this is called to display list of services that are linked (admin-functions.php)
	function vimeo_identify($item) {
		$entry=explode("|",$item);
		
		if (strpos($entry[3],"videos")) return "<a href='".$entry[3]."'>".$entry[2]." uploads</a>";
		if (strpos($entry[3],"likes")) return "<a href='".$entry[3]."'>".$entry[2]." likes</a>";
	}

	
	function vimeo_form() {
		print "Username: <input type='text' name='username'> <input type='submit' name='add' value='add'><br/>";
		print "uploads: <input type='checkbox' name='uploads' checked><br/>";
		print "likes: <input type='checkbox' name='favorites' checked><br/>";
	}
	



	// get service + profile information
	function vimeo_addProfile() {
			$serviceID=$_POST['addservice'];
			$username=$_POST['username'];
	
			// include uploads
			if (isset($_POST['uploads'])) {
				$rsslink="http://vimeo.com/".$username."/videos/rss";
				writeService($serviceID,$username,$rsslink);
			}
			
			// include favorites
			if (isset($_POST['favorites'])) {
				$rsslink="http://vimeo.com/".$username."/likes/rss";
				writeService($serviceID,$username,$rsslink);
			}
	}

	
	function vimeo_download($serviceid,$filename)  {
		$reader = new XMLReader();
		$reader->open($filename);

		set_time_limit(30);
		$count=0;
		
		$itemids=array();
		$continueParsing=true;

		while ($reader->read() && $continueParsing) {
				if ($reader->nodeType==XMLREADER::ELEMENT) {
				
					if ($reader->localName=="item") {
						$guid=null;
						$title=null;
						$mediaurl=null;
						$thumb=null;
						$link=null;
						$pubdate=null;
						$upddate=null;
						
						while ($reader->read()) {

							if ($reader->localName=="guid" && !$guid) {
								$reader->read();
								$guid=$reader->value;
							}

							/*
							if ($reader->localName=="link" && !$link) {
								$reader->read();
								$link=$reader->value;
							}
							*/
							
							
							if ($reader->localName=="pubDate" && !$pubdate) {
								$reader->read();
								$pubdate=$reader->value;
							}

							if ($reader->name=="title" && !$title) {
								$reader->read();
								$title=$reader->value;
							}
		

							if ($reader->name=="link" && !$mediaurl) {
								$reader->read();
								$mediaurl=$reader->value;
								// we only need the video code!
								$mediaurl=substr($mediaurl,strpos($mediaurl,"vimeo.com/")+10);
								$guid=$mediaurl;
							}
		
							
							if ($reader->localName=="item") {
								if ($guid) {
								
									$indx=date("YmdHis",strtotime($pubdate));
									/*
									if (strpos($filename,"uploads")) {
									} else {
										$indx=date("YmdHis",strtotime($upddate));
									}
									*/
									
									if (newGuid($guid)) {
										$item[$indx]->title=$title;
										$item[$indx]->post=$mediaurl;
										$item[$indx]->link=$link;
										$item[$indx]->guid=$guid;
										$item[$indx]->extra="|".$link."|".$title."|";
										$item[$indx]->pubDate=$pubdate;

										if (strpos($filename,"videos")) {
											$item[$indx]->extra.="uploads|";
										}
										
										if (strpos($filename,"likes")) {
											$item[$indx]->extra.="likes|";
										}

									} else $continueParsing=false;
									
								}
								break;
							}
						}
						
						//print $item[$guid]["title"]." : <img src='".$item[$guid]["media"]."'>";
					}
					set_time_limit(30);
				}
				
		}  
		
		insertEntries($item,"vimeo",$serviceid);
	}
?>